package com.mangguo.tingjian.statistics.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mangguo.tingjian.common.Result;
import com.mangguo.tingjian.sound.entity.StaticParam;
import com.mangguo.tingjian.statistics.dto.OperateGroupResult;
import com.mangguo.tingjian.sound.mapper.StaticParamMapper;
import com.mangguo.tingjian.statistics.dto.RetentionRateDTO;
import com.mangguo.tingjian.statistics.req.RetentionRateLineReq;
import com.mangguo.tingjian.statistics.service.IDwOpenAppService;
import com.mangguo.tingjian.user.dto.CountResult;
import com.mangguo.tingjian.util.CvsUtils;
import com.mangguo.tingjian.util.DateUtils;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author auto
 * @since 2019-12-01
 */
@RestController
@RequestMapping("/statistics")
@AllArgsConstructor
public class DwOpenAppController {

    private IDwOpenAppService dwOpenAppService;

    private StaticParamMapper staticParamMapper;

    @GetMapping("/getAllOperateGroup")
    public Result<List<OperateGroupResult>> getAllOperateGroup(RetentionRateLineReq retentionRateLineReq){
        List<OperateGroupResult> list  = new ArrayList<>();
        staticParamMapper.selectList(new QueryWrapper<StaticParam>().lambda().eq(StaticParam::getCode,"operate_group")).forEach(staticParam -> {
            OperateGroupResult operateGroupResult = new OperateGroupResult();
            operateGroupResult.setId(staticParam.getValue());
            operateGroupResult.setName(staticParam.getName());
            list.add(operateGroupResult);
        });
        return Result.success(list);
    }

    @PostMapping("/getRetentionRate")
    public Result<List<RetentionRateDTO>> getRetentionRateLine(@RequestBody RetentionRateLineReq retentionRateLineReq){
        return Result.success(dwOpenAppService.getRetentionRate(
                retentionRateLineReq));
    }

    @GetMapping("/getUserPlayTime/{start}/{end}/{operateGroup}")
    public Result<List<CountResult>> getUserPlayTime(@PathVariable("start") Integer start,
                                                     @PathVariable("end") Integer end,
                                                     @PathVariable("operateGroup")  Integer operateGroup){
        //初始化
        List<Integer> dateList = DateUtils.getBetweenDates(start, end);
        Map<Integer,CountResult> map = new HashMap<>();
        dateList.forEach(date -> {
            CountResult countResult = new CountResult();
            countResult.setMDate(date);
            countResult.setCount(0L);
            map.put(date, countResult);
        });
        List<CountResult> countResults = dwOpenAppService.getUserPlayTime(start, end , operateGroup);
        countResults.forEach(countResult -> {
            map.get(countResult.getMDate()).setCount(countResult.getCount());
        });

        List<CountResult> result = new ArrayList<>();
        dateList.forEach(date -> result.add(map.get(date)));
        return Result.success(result);
    }

    @GetMapping("/getUserPlayTime/{start}/{end}/{operateGroup}/download")
    public void getUserPlayTimeDownload(HttpServletResponse response,
                                        @PathVariable("start") Integer start,
                                        @PathVariable("end") Integer end,
                                        @PathVariable("operateGroup")  Integer operateGroup){
        //初始化
        List<Integer> dateList = DateUtils.getBetweenDates(start, end);
        Map<Integer,CountResult> map = new HashMap<>();
        dateList.forEach(date -> {
            CountResult countResult = new CountResult();
            countResult.setMDate(date);
            countResult.setCount(0L);
            map.put(date, countResult);
        });
        List<CountResult> countResults = dwOpenAppService.getUserPlayTime(start, end , operateGroup);
        countResults.forEach(countResult -> {
            map.get(countResult.getMDate()).setCount(countResult.getCount());
        });
        List<CountResult> results = new ArrayList<>();
        dateList.forEach(date -> results.add(map.get(date)));
        List<List<String>> list = new ArrayList<>();
        list.add(Arrays.asList("日期", "人数"));
        for(CountResult countResult : results){
            list.add(Arrays.asList(countResult.getMDate().toString(),
                    countResult.getCount().toString()));
        }
        CvsUtils.downloadCvs(response, "用户留存率分析", list);
    }



    /**
     * 下载
     * @return
     */
    @GetMapping("/getRetentionRate/download")
    public void getRetentionRateLineDownload(HttpServletResponse response,
                                             @RequestParam Integer start,
                                             @RequestParam Integer end,
                                             @RequestParam String tableName,
                                             @RequestParam List<Integer> city,
                                             @RequestParam List<Integer> province,
                                             @RequestParam List<String> osid,
                                             @RequestParam List<String> channel){
        RetentionRateLineReq retentionRateLineReq = new RetentionRateLineReq();
        retentionRateLineReq.setStart(start);
        retentionRateLineReq.setEnd(end);
        retentionRateLineReq.setTableName(tableName);
        retentionRateLineReq.setCity(city);
        retentionRateLineReq.setProvince(province);
        retentionRateLineReq.setOsid(osid);
        retentionRateLineReq.setChannel(channel);
        List<RetentionRateDTO> retentionRateDTOS = dwOpenAppService.getRetentionRate(retentionRateLineReq);
        List<List<String>> list = new ArrayList<>();
        list.add(Arrays.asList("日期", "人数", "第一天", "第二天", "第三天", "第四天", "第五天", "第六天", "第七天", "第十四天", "第三十天"));
        for(RetentionRateDTO retentionRateDTO : retentionRateDTOS){
            list.add(Arrays.asList(retentionRateDTO.getMDate().toString(),
                    retentionRateDTO.getPeople().toString(),
                    retentionRateDTO.getRetentionD1().toString(),
                    retentionRateDTO.getRetentionD2().toString(),
                    retentionRateDTO.getRetentionD3().toString(),
                    retentionRateDTO.getRetentionD4().toString(),
                    retentionRateDTO.getRetentionD5().toString(),
                    retentionRateDTO.getRetentionD6().toString(),
                    retentionRateDTO.getRetentionD7().toString(),
                    retentionRateDTO.getRetentionD14().toString(),
                    retentionRateDTO.getRetentionD30().toString()));
        }
        CvsUtils.downloadCvs(response, "留存率统计", list);
    }



}
